package com.crimsec.utils.snmp.model;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

public class AbstractEntity {

    @XmlType
    public static class TransientCollectionDecorator {

        public String genericName;
        public String methodName;
        public String collectionType;

        @Override
        public String toString() {
            return "TransientCollectionDecorator[" + "genericName=" + genericName +
                    ", methodName=" + methodName + ", collectionType=" + collectionType + ']';
        }
        
    }
    // 
    @XmlElementWrapper(name = "decorators")
    @XmlElement(name = "decorator")
    public Collection<TransientCollectionDecorator> decorators;

    public AbstractEntity() {
        setupDecorators();
    }

    private void setupDecorators() {
        Class clz = this.getClass();
        Method[] declaredMethods = clz.getDeclaredMethods();
        for (Method m : declaredMethods) {
            Class<?> rt = m.getReturnType();
            if (rt.getName().equals("java.util.Collection")) {
                if (m.isAnnotationPresent(XmlTransient.class)) {
                    addDecorator(m);
                }
            }

        }
    }

    private void addDecorator(Method m) {
        if (decorators == null) {
            this.decorators = new ArrayList<TransientCollectionDecorator>(5);
        }
      
        TransientCollectionDecorator decorator = new TransientCollectionDecorator();
        decorator.genericName = m.toGenericString();
        decorator.methodName = m.getName();
        decorator.collectionType = m.getGenericReturnType().toString();
        decorators.add(decorator);
    }
}
